package com.lvy.hczn.front.system.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.system.domain.system.dto.SysDictTypeDto;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lvy.hczn.front.common.core.domain.entity.SysDictData;
import com.lvy.hczn.front.common.core.text.Convert;
import com.lvy.hczn.front.common.utils.DictUtils;
import com.lvy.hczn.front.system.mapper.SysDictDataMapper;
import com.lvy.hczn.front.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService {
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private ProducerUtil producerUtil;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据
     *
     * @param ids 需要删除的数据
     */
    @Override
    public void deleteDictDataByIds(String ids) throws Exception {
        Long[] dictCodes = Convert.toLongArray(ids);
        List<SysDictData> sysDictDataList = new ArrayList<>();
        for (Long dictCode : dictCodes) {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);

            //初始化发送数据
            data.setStatus(Constants.DEL_FLAG_DELETE_TRUE);
            sysDictDataList.add(data);
        }

        if (!sysDictDataList.isEmpty()) {
            sendSysDictDataToMq(sysDictDataList);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data) throws Exception {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0) {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
            sendSysDictDataToMq(dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data) throws Exception {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0) {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
            sendSysDictDataToMq(dictDatas);
        }
        return row;
    }

    /**
     * 发送数据到队列
     *
     * @param dictDatas
     * @return void
     * @author yw
     * @date 2023-05-13 15:56:18
     */
    public void sendSysDictDataToMq(List<SysDictData> dictDatas) throws Exception {
        //是否允许向仓库发数据：false:不允许；true:允许
        List<SysDictData> result = DictUtils.getDictCache(Constants.SYS_DICT_KEY_SEND_MSG);
        if (result != null && !result.isEmpty()) {
            for (SysDictData sysDictData : result) {
                if (sysDictData.getDictValue().equals("true")) {
                    SysDictTypeDto dto = new SysDictTypeDto();
                    dto.setTypeOfDictData(true);
                    dto.setList(dictDatas);
                    producerUtil.sendAsyncMsg(MqConstants.TOPIC_DICT, null, JSONUtil.toJsonStr(dto).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
                }
            }
        }
    }
}
